package com.cloudsigns.factorymanager2.project.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.BaseActivity;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.customview.DateViewDialog;
import com.cloudsigns.factorymanager2.produce.activity.ProduceAddProductInfomationActivity;
import com.cloudsigns.factorymanager2.produce.adapter.Produce_ProductInfoAdapter;
import com.cloudsigns.factorymanager2.produce.bean.Produce_ProductBean;
import com.cloudsigns.factorymanager2.project.customview.GeneralPurposePoupWindow;
import com.cloudsigns.factorymanager2.project.iview.IAddProduceOrderView;
import com.cloudsigns.factorymanager2.project.myinerface.ItemClickListener;
import com.cloudsigns.factorymanager2.project.myinerface.ItemRemoveListener;
import com.cloudsigns.factorymanager2.project.presenter.AddProduceOrderPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;


public class ProductionTaskOrderActivity1 extends BaseActivity implements ItemRemoveListener, ItemClickListener, IAddProduceOrderView {
    private String TAG = ProductionTaskOrderActivity1.class.getSimpleName();
    private RelativeLayout rl_right;
    private ImageView img_back;
    private TextView tv_title;
    private EditText edt_project_name, edt_engineering_name, edt_work_sheet_category, edt_level, edt_completion_date;
    private ImageView img_add_product;
    private ImageView img_completion_date;
    private ImageView img_work_sheet_category, img_level;
    private ListView lv;
    private Button btn_save;
    private CheckBox cb_isHurry;

    private List<Produce_ProductBean> productBeanList = new ArrayList<>();
    private Produce_ProductInfoAdapter adapter;

    private PopupWindow mPopupWindow;

    private GeneralPurposePoupWindow generalPurposePoupWindow;
    private String categorys[] = {"生产工单", "打样工单", "免费工单"};

    private GeneralPurposePoupWindow generalPurposePoupWindow1;
    private String level[] = {"非常重要", "比较重要", "一般重要", "正常"};

    private String clickType;
    private String isHurry = "NOT_HURRY";
    private String levelStr;

    private AddProduceOrderPresenter addProduceOrderPresenter;

    @Override
    protected void onBaseCreate(Bundle savedInstanceState) {
        setContentView(R.layout.layout_production_task_order1);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        img_back = findViewById(R.id.img_back);
        rl_right = findViewById(R.id.rl_right);
        tv_title = findViewById(R.id.tv_title);
        edt_project_name = findViewById(R.id.edt_project_name);
        edt_engineering_name = findViewById(R.id.edt_engineering_name);
        edt_work_sheet_category = findViewById(R.id.edt_work_sheet_category);
        edt_level = findViewById(R.id.edt_level);
        img_add_product = findViewById(R.id.img_add_product);
        edt_completion_date = findViewById(R.id.edt_completion_date);
        img_completion_date = findViewById(R.id.img_completion_date);
        img_work_sheet_category = findViewById(R.id.img_work_sheet_category);
        img_level = findViewById(R.id.img_level);
        lv = findViewById(R.id.lv);
        btn_save = findViewById(R.id.btn_save);
        cb_isHurry = findViewById(R.id.cb_isHurry);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        tv_title.setText("生产任务单");
        cb_isHurry.setChecked(false);

        adapter = new Produce_ProductInfoAdapter(this, TAG);
        adapter.setRemoveListener(this);
        adapter.setData(productBeanList);
        lv.setAdapter(adapter);

        addProduceOrderPresenter = new AddProduceOrderPresenter(this);
    }

    @Override
    public void setListener() {
        img_back.setOnClickListener(this);
        rl_right.setOnClickListener(this);
        img_add_product.setOnClickListener(this);
        img_completion_date.setOnClickListener(this);
        img_work_sheet_category.setOnClickListener(this);
        img_level.setOnClickListener(this);
        btn_save.setOnClickListener(this);

        cb_isHurry.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (isChecked) {
                    isHurry = "HURRY";
                } else {
                    isHurry = "NOT_HURRY";
                }
            }
        });
    }

    // 选择日期
    private void showDateDialog() {
        DateViewDialog endDateChooseDialog = new DateViewDialog(this,
                new DateViewDialog.DateChooseInterface() {
                    @Override
                    public void getDateTime(String time, boolean longTimeChecked) {
                        edt_completion_date.setText(time);
                    }
                });
        endDateChooseDialog.setTimePickerGone(true);
        Calendar dateAndTime = Calendar.getInstance(Locale.getDefault());
        endDateChooseDialog.setDateDialogTitle(dateAndTime.get(Calendar.YEAR) + "-" + (dateAndTime.get(Calendar.MONTH) + 1) + "-" + dateAndTime.get(Calendar.DAY_OF_MONTH));
        endDateChooseDialog.showDateChooseDialog();
    }

    // 工单类型
    private void showOrderTypesWindow(View anchor) {
//        generalPurposePoupWindow = new GeneralPurposePoupWindow(this, categorys);
//        generalPurposePoupWindow.setItemClick(this);
//        generalPurposePoupWindow.showPopupWindow(anchor);

        View contentView = LayoutInflater.from(this).inflate(R.layout.popupwindow_produce_order_type, null);
        LinearLayout ll_A = contentView.findViewById(R.id.ll_A);
        LinearLayout ll_B = contentView.findViewById(R.id.ll_B);
        LinearLayout ll_C = contentView.findViewById(R.id.ll_C);
        LinearLayout ll_D = contentView.findViewById(R.id.ll_D);
        LinearLayout ll_E = contentView.findViewById(R.id.ll_E);

        ll_A.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                edt_work_sheet_category.setText("标准工单");
                mPopupWindow.dismiss();
            }
        });

        ll_B.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                edt_work_sheet_category.setText("返工工单");
                mPopupWindow.dismiss();
            }
        });

        ll_C.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                edt_work_sheet_category.setText("报废工单");
                mPopupWindow.dismiss();
            }
        });

        ll_D.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                edt_work_sheet_category.setText("打样工单");
                mPopupWindow.dismiss();
            }
        });

        ll_E.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                edt_work_sheet_category.setText("免费工单");
                mPopupWindow.dismiss();
            }
        });

        mPopupWindow = new PopupWindow(contentView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
        mPopupWindow.setTouchable(true);
        /* 设置点击menu以外其他地方以及返回键退出 */
        mPopupWindow.setFocusable(true);
        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        mPopupWindow.showAsDropDown(anchor);
    }

    // 工单级别
    private void showOrderLevelWindow(View anchor) {
//        generalPurposePoupWindow1 = new GeneralPurposePoupWindow(this, level);
//        generalPurposePoupWindow1.setItemClick(this);
//        generalPurposePoupWindow1.showPopupWindow(anchor);
        View contentView = LayoutInflater.from(this).inflate(R.layout.popupwindow_produce_order_level, null);
        LinearLayout ll_one = contentView.findViewById(R.id.ll_one);
        LinearLayout ll_two = contentView.findViewById(R.id.ll_two);
        LinearLayout ll_three = contentView.findViewById(R.id.ll_three);
        LinearLayout ll_four = contentView.findViewById(R.id.ll_four);

        ll_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                edt_level.setText("非常重要");
                mPopupWindow.dismiss();
            }
        });

        ll_two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                edt_level.setText("比较重要");
                mPopupWindow.dismiss();
            }
        });

        ll_three.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                edt_level.setText("一般重要");
                mPopupWindow.dismiss();
            }
        });

        ll_four.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                edt_level.setText("正常");
                mPopupWindow.dismiss();
            }
        });


        mPopupWindow = new PopupWindow(contentView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
        mPopupWindow.setTouchable(true);
        /* 设置点击menu以外其他地方以及返回键退出 */
        mPopupWindow.setFocusable(true);
        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        mPopupWindow.showAsDropDown(anchor);
    }

    // 保存
    private void save() {
        if (TextUtils.isEmpty(edt_project_name.getText().toString())) {
            Toast.makeText(ProductionTaskOrderActivity1.this, "请输入项目名称", Toast.LENGTH_SHORT).show();
        } else if (TextUtils.isEmpty(edt_engineering_name.getText().toString())) {
            Toast.makeText(ProductionTaskOrderActivity1.this, "请输入工程名称", Toast.LENGTH_SHORT).show();
        } else if (TextUtils.isEmpty(edt_work_sheet_category.getText().toString())) {
            Toast.makeText(ProductionTaskOrderActivity1.this, "请选择工单类型", Toast.LENGTH_SHORT).show();
        } else if (TextUtils.isEmpty(edt_level.getText().toString())) {
            Toast.makeText(ProductionTaskOrderActivity1.this, "请选择工单级别", Toast.LENGTH_SHORT).show();
        } else if (TextUtils.isEmpty(edt_completion_date.getText().toString())) {
            Toast.makeText(ProductionTaskOrderActivity1.this, "请选择完成日期", Toast.LENGTH_SHORT).show();
        } else {
            addProduceOrderPresenter.addProduceOrder();
        }
    }

    @Override
    public void widgetClick(View view) {
        Intent mIntent;
        switch (view.getId()) {
            // 返回
            case R.id.img_back:
                finish();
                break;
            // 更多
            case R.id.rl_right:
                mIntent = new Intent(ProductionTaskOrderActivity1.this, EditProductTaskOrderCategoryActivity.class);
                startActivity(mIntent);
                break;
            // 工单类型
            case R.id.img_work_sheet_category:
                hideSoftInputView();
                showOrderTypesWindow(edt_work_sheet_category);
                clickType = "category";
                break;
            // 工单级别
            case R.id.img_level:
                hideSoftInputView();
                showOrderLevelWindow(edt_level);
                clickType = "level";
                break;
            //日期
            case R.id.img_completion_date:
                showDateDialog();
                break;
            // 产品清单
            case R.id.img_add_product:
                // mIntent = new Intent(ProductionTaskOrderActivity1.this, AddProductInfomationActivity1.class);
                mIntent = new Intent(ProductionTaskOrderActivity1.this, ProduceAddProductInfomationActivity.class);
                mIntent.putExtra("type", "productionTaskOrder");
                startActivityForResult(mIntent, ConstantUtil.ADD_PRODUCT_INFOMATION);
                break;
            // 保存
            case R.id.btn_save:
                if ("非常重要".equals(edt_level.getText().toString())) {
                    levelStr = "VERY_IMPORTANT";
                } else if ("比较重要".equals(edt_level.getText().toString())) {
                    levelStr = "MORE_IMPORTANT";
                } else if ("一般重要".equals(edt_level.getText().toString())) {
                    levelStr = "GENERALLY_IMPORTANT";
                } else if ("正常".equals(edt_level.getText().toString())) {
                    levelStr = "NORMAL";
                }
                save();
                break;
        }
    }

    @Override
    public void remove(int positon) {  //列表中删除事件
        if (positon >= 0 && positon < productBeanList.size()) {
            productBeanList.remove(positon);
            adapter.setData(productBeanList);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void remove(View view) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case ConstantUtil.ADD_PRODUCT_INFOMATION: //添加产品信息
                if (data != null) {
                    Produce_ProductBean productBean = (Produce_ProductBean) data.getSerializableExtra(ConstantUtil.PRODUCTBEAN);
                    if (productBean != null) {
                        productBeanList.add(productBean);
                        adapter.setData(productBeanList);
                        adapter.notifyDataSetChanged();
                    }
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void itemClick(View view, int position) {
//        if ("category".equals(clickType)) {
//            edt_work_sheet_category.setText(categorys[position]);
//        } else if ("level".equals(clickType)) {
//            edt_level.setText(level[position]);
//        }
    }


    @Override
    public long getCompleteDate() {
        long mills = DateUtil.getMillis(edt_completion_date.getText().toString().trim());
        String date = DateUtil.getYyyy_Mm_Dd(mills);
        return mills;
    }

    @Override
    public String getEngineeringName() {
        return edt_engineering_name.getText().toString().trim();
    }

    @Override
    public String getIsHurry() {
        return isHurry;
    }

    @Override
    public String getLevel() {
        return levelStr;
    }

    @Override
    public List<Produce_ProductBean> getProductList() {
        return productBeanList;
    }

    @Override
    public String getProjectName() {
        return edt_project_name.getText().toString().trim();
    }

    @Override
    public String getType() {
        return edt_work_sheet_category.getText().toString().trim();
    }

    @Override
    public void showSuccess(String orderId) {
        Toast.makeText(this, "保存成功" + orderId, Toast.LENGTH_SHORT).show();
        Intent mIntent = new Intent();
        mIntent.putExtra(ConstantUtil.KEY_1, true);
        setResult(ConstantUtil.CREATED_ORDER, mIntent);
        finish();
    }

    @Override
    public void showError(String desc) {
        Toast.makeText(this, "保存失败" + desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
